$(function(){
    //修改试卷
    $('#papers-edit').on('click', function() {
    	var li=$("ul").find(".active");//获得所有被选中的li
		 if(li.length===1){
			 var qid=li.attr("id");
			 window.location.href="/paperManager/edit?id="+qid;
			 
		 }else{
			 zebra_Dialog_warning("请选择一条数据!");
		 }
    });
    $('#papers-downdata').on('click', function() {
    	var li=$("ul").find(".active");//获得所有被选中的li
    	
    	if(li.length===1){
    		var qid=li.attr("id");
    		var paperName=li.find(".paper-a").text();
    		var pname=paperName.substring(5);
    		$.ajax({
	             type: "post",
	             url: "/paperManager/outPapers",
	             data:{pids:qid,paperName:pname},
	             success: function(){
	            	 exportDoc(qid,pname);
	            	 zebra_Dialog_success("导出成功!");
	            }
	         });
    		
    	}else{
    		zebra_Dialog_warning("请选择一条数据!");
    	}
    });
    //导出试卷
    /**
     * $('#papers-downdata').on('click', function() {
    	 var ids="";
		 var li=$("ul").find(".active");//获得所有被选中的li
		li.each(function(){
			 var qid=$(this).find(":checkbox").val();
			 ids+=qid+",";
			
		});
		
    	if(ids.length > 0){
    		$.ajax({
	             type: "post",
	             url: "/paperManager/outPapers",
	             data:{pids:ids},
	             success: function(){
	            	 var idstr=ids.split(",");
	            	for (var i = 0; i < idstr.length; i++) {
	            		 exportDoc(idstr[i]);
					}
	            	zebra_Dialog_success("导出成功!");
	            	
	            }
	         });
    	}
    });*/
    
    //导入试卷
    $('#papers-updata').on('click', function() {
    	window.location.href='/paperManager/papers-updata';
    });
    
    
    
    
});
function papersDelete(){
	
	var ids="";
	 var li=$("ul").find(".active");//获得所有被选中的li
	 var flag=false;
	li.each(function(){
		var status=$(this).find(":checkbox").val();
		if("0"===status){
			flag=true;
		}
		var qid=$(this).attr("id");
		ids+=qid+",";
	});
	if(flag){
		zebra_Dialog_warning("所选试卷含有被使用,不能删除!");
		return false;
	}
	if(ids===""){
		zebra_Dialog_warning("请选择数据!");
		return false;
	}
	 var diag = new $.Zebra_Dialog({
         source:  {'ajax': '/questions/deleteAlert.html'},
         width:300,
         title:'提示',
         'buttons' : [ {
				caption : '确定',
				callback : function() {
					$.ajax({
						url: "/paperManager/deletePaper",
						type: "POST",
						data: {"ids": ids},
						success: function(v){
							if(v==="1"){
								 $.Zebra_Dialog('<div class="prompt"><div class="success-icon"></div><div class="success-title">删除成功!</div></div>', {
						                'title': '提示',
						                'buttons' : [ {
											caption : '确定',
											callback : function() {
												window.location.href="/paperManager/papers";
											}
										} ]
						            });
							}
						},
						error: function () {
							zebra_Dialog_warning("删除失败!");
					    }
					});
				}},
				{
					caption : '取消',
					callback : function() {
						window.location.href="/paperManager/papers";
					}
			} ]
     }); 
	
	
}

function exportDoc(ids,paperName){
	 var url = "/paperManager/export?pids="+ids+"&paperName="+encodeURI(encodeURI(paperName));
		  var xhr = new XMLHttpRequest();
		  xhr.open('POST', url, true);
		  xhr.responseType = "blob";
		  xhr.onreadystatechange = function () {
			if (xhr.readyState === 4 &&( xhr.status === 200||xhr.status === 201)) {
						var blob = new Blob([xhr.response], {type: 'application/octet-stream'});
						saveAs(blob,paperName+".docx");
			}
		};
		  xhr.send();
}
